package com.qsmx.qingqiuticket.app.delegate.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qsmx.qingqiuticket.app.R;
import com.qsmx.qingqiuticket.app.delegate.holder.MemPackageHolder;
import com.qsmx.qingqiuticket.app.delegate.holder.PackageTicketListHolder;
import com.qsmx.qingqiuticket.app.entity.GroupPackageListEntity;
import com.qsmx.qingqiuticket.app.entity.MemTicketListEntity;
import com.qsmx.qingqiuticket.app.entity.PackageListEntity;

import java.util.List;

public class MemPackageListAdapter extends RecyclerView.Adapter<MemPackageHolder> {

    private Context mContext;
    private List<MemTicketListEntity.BodyBean.MachineTicketInfosBean> mData = null;

    private boolean isCanPrint = true;

    //定义接口
    public static interface OnItemClickListener {
        void onClick(View view, int position);

    }

    //声明自定义的监听接口
    private MemPackageListAdapter.OnItemClickListener monItemClickListener = null;

    //提供set方法
    public void setonItemClickListener(MemPackageListAdapter.OnItemClickListener listener) {
        this.monItemClickListener = listener;
    }

    public MemPackageListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<MemTicketListEntity.BodyBean.MachineTicketInfosBean> mData, boolean isCanPrint) {
        this.mData = mData;
        this.isCanPrint = isCanPrint;
    }

    public List<MemTicketListEntity.BodyBean.MachineTicketInfosBean> getData() {
        return mData;
    }

    @NonNull
    @Override
    public MemPackageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MemPackageHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_mem_package_list, parent, false));

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull MemPackageHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvPackageName.setText(mData.get(position).getPackageName());
        holder.tvPackageTips.setText(mData.get(position).getPackageSubTitle());
        holder.tvSessionInfo.setText("场次" + mData.get(position).getSessionNo() + " " + mData.get(position).getSessionDate() + " " + mData.get(position).getSessionStartTime() + "-" + mData.get(position).getSessionEndTime());
        MemPackageTicketListAdapter mAdapter = new MemPackageTicketListAdapter(mContext);
        if (!mData.get(position).getMachineTicketInfos().isEmpty()) {
            mAdapter.setData(mData.get(position).getMachineTicketInfos());
        }
        holder.rlvPackageTicketList.setAdapter(mAdapter);
        holder.rlvPackageTicketList.setLayoutManager(new LinearLayoutManager(mContext));

        if (isCanPrint) {
            holder.tvGetNow.setVisibility(View.VISIBLE);
        } else {
            holder.tvGetNow.setVisibility(View.GONE);
        }

        holder.tvGetNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monItemClickListener.onClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mData == null) {
            return 0;
        } else {
            return mData.size();
        }
    }
}
